﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.Core;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;
namespace ERP.Manager.SQLServerProvider
{
    public class ECS_sellstockinfoRepository : IECS_sellstockinfoRepository
    {
        #region 添加商品发货信息的方法
        public int add_ecs_sellstockinfo(ECS_sellstockinfo ecs_sellstock)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_ECS_SELLSTOCKINFO,
                new SqlParameter("@sell_goods_id", ecs_sellstock.sell_goods_id),
                new SqlParameter("@sell_goods_name", ecs_sellstock.sell_goods_name),
                new SqlParameter("@sell_goods_sn", ecs_sellstock.sell_goods_sn),
                new SqlParameter("@sell_goods_type", ecs_sellstock.sell_goods_type.cat_id),
                new SqlParameter("@sell_brand_id", ecs_sellstock.sell_brand_id),
                new SqlParameter("@sell_order_number", ecs_sellstock.sell_order_number),
                new SqlParameter("@sell_Delivery_number", ecs_sellstock.sell_Delivery_number),
                new SqlParameter("@sell_consignment_mode", ecs_sellstock.sell_consignment_mode),
                new SqlParameter("@sell_consignment_web", ecs_sellstock.sell_consignment_web),
                new SqlParameter("@sell_attr_values", ecs_sellstock.sell_attr_values),
                new SqlParameter("@sell_number", ecs_sellstock.sell_number),
                new SqlParameter("@sell_price", ecs_sellstock.sell_price),
                new SqlParameter("@sell_end_time", ecs_sellstock.sell_end_time),
                new SqlParameter("@sell_remark", ecs_sellstock.sell_remark));
            return rowcount;
        }
        #endregion

        #region 查询所有发货的商品信息
        public List<ECS_sellstockinfo> all_ecs_sellstockinfo()
        {
            List<ECS_sellstockinfo> list = new List<ECS_sellstockinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO);
            try
            {
                while (read.Read())
                {
                    ECS_sellstockinfo ecs_sell = new ECS_sellstockinfo();
                    ecs_sell.sell_order_number = read["sell_order_number"].ToString();
                    ecs_sell.sell_Delivery_number = read["sell_Delivery_number"].ToString();
                    ecs_sell.sell_goods_name = read["sell_goods_name"].ToString();
                    ecs_sell.sell_consignment_mode = read["sell_consignment_mode"].ToString();
                    ecs_sell.sell_consignment_web = read["sell_consignment_web"].ToString();
                    ecs_sell.sell_attr_values = read["sell_attr_values"].ToString();
                    ecs_sell.sell_number = int.Parse(read["sell_number"].ToString());
                    ecs_sell.sell_price = decimal.Parse(read["sell_price"].ToString());
                    ecs_sell.sell_end_time = DateTime.Parse(read["sell_end_time"].ToString());
                    list.Add(ecs_sell);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 分页查询发送商品的货物
        public List<ECS_sellstockinfo> all_ecs_sellstockinfo_page(int pagecount)
        {
            List<ECS_sellstockinfo> list = new List<ECS_sellstockinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_PAGE,
                new SqlParameter("@pagecount", pagecount));
            try
            {
                while (read.Read())
                {
                    ECS_sellstockinfo ecs_sell = new ECS_sellstockinfo();
                    ecs_sell.sell_order_number = read["sell_order_number"].ToString();
                    ecs_sell.sell_Delivery_number = read["sell_Delivery_number"].ToString();
                    ecs_sell.sell_goods_name = read["sell_goods_name"].ToString();
                    ecs_sell.sell_consignment_mode = read["sell_consignment_mode"].ToString();
                    ecs_sell.sell_consignment_web = read["sell_consignment_web"].ToString();
                    ecs_sell.sell_attr_values = read["sell_attr_values"].ToString();
                    ecs_sell.sell_number = int.Parse(read["sell_number"].ToString());
                    ecs_sell.sell_price = decimal.Parse(read["sell_price"].ToString());
                    ecs_sell.sell_end_time = DateTime.Parse(read["sell_end_time"].ToString());
                    list.Add(ecs_sell);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion


        #region 查询发货商品总共记录数
        public int all_ecs_sellstockinfo_page_count()
        {
            var rowcount = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_PAGE_COUNT);
            return int.Parse(rowcount.ToString());
        }
        #endregion

        #region 根据发货时间搜索得到的发货记录信息
        public List<ECS_sellstockinfo> all_ecs_sellstockinfo_page_and_sell_end_time_page(int pagecount, string sell_goods_name, string begin_time, string end_time)
        {
            List<ECS_sellstockinfo> list = new List<ECS_sellstockinfo>();
            SqlDataReader read = null;
            if (begin_time == "" || end_time == "")
            {
                read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_AND_SELL_GOODS_NAME_PAGE,
               new SqlParameter("@pagecount", pagecount),
               new SqlParameter("@sell_goods_name", "%" + sell_goods_name.Trim() + "%"));
            }
            else if (sell_goods_name == "")
            {
                read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_AND_SELL_END_TIME_PAGE,
                            new SqlParameter("@pagecount", pagecount),
                            new SqlParameter("@begin_time", begin_time),
                            new SqlParameter("@end_time", end_time));
            }
            else
            {
                read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_AND_SELL_GOODS_NAME_NAD_SELL_END_TIME_PAGE,
                            new SqlParameter("@pagecount", pagecount),
                            new SqlParameter("@sell_goods_name", "%" + sell_goods_name.Trim() + "%"),
                            new SqlParameter("@begin_time", begin_time),
                            new SqlParameter("@end_time", end_time));
            }
            try
            {
                while (read.Read())
                {
                    ECS_sellstockinfo ecs_sell = new ECS_sellstockinfo();
                    ecs_sell.sell_order_number = read["sell_order_number"].ToString();
                    ecs_sell.sell_Delivery_number = read["sell_Delivery_number"].ToString();
                    ecs_sell.sell_goods_name = read["sell_goods_name"].ToString();
                    ecs_sell.sell_consignment_mode = read["sell_consignment_mode"].ToString();
                    ecs_sell.sell_consignment_web = read["sell_consignment_web"].ToString();
                    ecs_sell.sell_attr_values = read["sell_attr_values"].ToString();
                    ecs_sell.sell_number = int.Parse(read["sell_number"].ToString());
                    ecs_sell.sell_price = decimal.Parse(read["sell_price"].ToString());
                    ecs_sell.sell_end_time = DateTime.Parse(read["sell_end_time"].ToString());
                    list.Add(ecs_sell);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 根据发货时间搜索得到的发货记录信息的总数
        public int all_ecs_sellstockinfo_page_and_sell_end_time_count(string sell_goods_name, string begin_time, string end_time)
        {
            object rowcount = null;
            if (begin_time == "" || end_time == "")
            {
                rowcount = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_AND_SELL_GOODS_NAME_PAGE_COUNT,
                   new SqlParameter("@sell_goods_name", "%" + sell_goods_name.Trim() + "%"));
            }
            else if (sell_goods_name == "")
            {
                rowcount = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_AND_SELL_END_TIME_PAGE_COUNT,
                   new SqlParameter("@begin_time", begin_time),
                   new SqlParameter("@end_time", end_time));
            }
            else
            {
                rowcount = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_ECS_SELLSTOCKINFO_AND_SELL_GOODS_NAME_NAD_SELL_END_TIME_PAGE_COUNT,
                      new SqlParameter("@sell_goods_name", "%" + sell_goods_name.Trim() + "%"),
                     new SqlParameter("@begin_time", begin_time),
                     new SqlParameter("@end_time", end_time));
            }
            return int.Parse(rowcount.ToString());
        }
        #endregion
    }
}
